//
//  RHMePlantTopoDevItemCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/6.
//

#import "RHMePlantTopoDevItemCell.h"
#import "RHDeviceModel.h"

@interface RHMePlantTopoDevItemCell()
@property (nonatomic, strong) QMUILabel *statusLab;

@property (nonatomic, strong) QMUILabel *deviceIdLab;

@property (nonatomic, strong) QMUILabel *socLab;

@property (nonatomic, strong) UIImageView *icon;

@property (nonatomic, strong) UIView *shadowView;
@end

@implementation RHMePlantTopoDevItemCell
-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHDeviceModel *)model
{
    _model = model;
    self.deviceIdLab.text = model.deviceId;
    self.socLab.text = [NSString stringWithFormat:@"SOC:%@%%",[NSDecimalNumber decimalStringWithDoubleString:model.soc nullStr:@"--"]];
    self.statusLab.hidden = YES;
    //在线状态
    if([model.status isEqualToString:@"1"]){
        //离线
        self.statusLab.hidden = NO;
        self.statusLab.textColor = kCOLOR_HEX(@"#8B8D92");
        self.statusLab.text = @"离线".rh_localized;
    }else if([model.status isEqualToString:@"0"]){
        //在线
        self.statusLab.textColor = kCOLOR_HEX(@"");
        self.statusLab.hidden = YES;
    }
    
    //故障状态
   
    if(model.normal == 2){
        //故障
        self.statusLab.hidden = NO;
        self.statusLab.textColor = kCOLOR_HEX(@"#F95454");
        self.statusLab.text = @"故障".rh_localized;
    }else if(model.normal == 1){
        self.statusLab.hidden = NO;
        self.statusLab.textColor = kCOLOR_HEX(@"#F0A330");
        self.statusLab.text = @"告警".rh_localized;
    }
    
    [self.statusLab sizeToFit];
    [self.statusLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(self.statusLab.width+20);
    }];
    self.shadowView.hidden = self.statusLab.isHidden;
    
    [self setDeviceSoc];
}

-(void)setDeviceSoc
{
    NSInteger soc = [self.model.soc integerValue];
    NSString *imageStr = @"plant_topo_device_0";
    if (soc>0&&soc<15) {
        imageStr = @"plant_topo_device_10";
    }else if (soc>=15&&soc<25){
        imageStr = @"plant_topo_device_20";
    }else if (soc>=25&&soc<35){
        imageStr = @"plant_topo_device_30";
    }else if (soc>=35&&soc<45){
        imageStr = @"plant_topo_device_40";
    }else if (soc>=45&&soc<55){
        imageStr = @"plant_topo_device_50";
    }else if (soc>=55&&soc<65){
        imageStr = @"plant_topo_device_60";
    }else if (soc>=65&&soc<75){
        imageStr = @"plant_topo_device_70";
    }else if (soc>=75&&soc<85){
        imageStr = @"plant_topo_device_80";
    }else if (soc>=85&&soc<100){
        imageStr = @"plant_topo_device_90";
    }else if (soc>=100){
        imageStr = @"plant_topo_device_100";
    }
    self.icon.image = [UIImage imageNamed:imageStr];
}

-(void)setupUI
{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = kCOLOR_HEX(@"#FCFBFB");
    bgView.layer.borderColor = kBorderColor.CGColor;
    bgView.layer.borderWidth = 1;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.height.offset(kUI_WINDOW_W*0.5);
    }];
   
    
    
    _icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"plant_topo_device_0"]];
    [bgView addSubview:_icon];
    [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(32, 46)));
        make.centerX.mas_offset(0);
        make.top.mas_offset(15);
    }];
    
    _deviceIdLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
    _deviceIdLab.text = @"--";
    [bgView addSubview:_deviceIdLab];
    [_deviceIdLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.icon.mas_bottom).offset(15);
        make.left.right.mas_offset(0);
    }];
    
    _socLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentCenter];
    _socLab.text = @"--";
    [bgView addSubview:_socLab];
    [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.deviceIdLab.mas_bottom).offset(5);
        make.left.right.mas_offset(0);
    }];
    
    _statusLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentCenter];
    _statusLab.backgroundColor = UIColor.whiteColor;
    _statusLab.layer.cornerRadius = 10;
    _statusLab.layer.masksToBounds = YES;
    [bgView addSubview:_statusLab];
    [_statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(20);
    }];
    
    UIView *shadowView = kView;
    shadowView.backgroundColor = UIColor.whiteColor;
    shadowView.layer.shadowColor = kCOLOR_HEX_A(@"#000000", 0.5).CGColor;
    shadowView.layer.shadowRadius = 5;
    shadowView.layer.shadowOpacity = 0.5;
    shadowView.layer.cornerRadius = 10;
    shadowView.layer.shadowOffset = CGSizeMake(0, 0.5);
    [bgView addSubview:shadowView];
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(weakSelf.statusLab);
    }];
    self.shadowView = shadowView;
    [bgView bringSubviewToFront:weakSelf.statusLab];
}
@end
